package tld.test.interpreter.logic
{
	import tld.test.interpreter.Context;
	import tld.test.interpreter.IContext;
	
	public class LogicContext implements IContext
	{
		private var _parent:IContext;
		
		private const _names:Object =
		{
			">" : gt,
			">=" : geq,
			"<" : lt,
			"<=" : leq,
			"!" : not,
			"=" : eq
		};
		
		public function LogicContext(parent:IContext)
		{
			super();
			this._parent = parent;
		}
		
		private function gt(a:Number, b:Number):Boolean
		{
			return a > b;
		}
		
		private function geq(a:Number, b:Number):Boolean
		{
			return a >= b;
		}
		
		private function lt(a:Number, b:Number):Boolean
		{
			return a < b;
		}
		
		private function leq(a:Number, b:Number):Boolean
		{
			return a <= b;
		}
		
		private function eq(a:Object, b:Object):Boolean
		{
			return a == b;
		}
		
		private function not(x:Object):Boolean
		{
			return !x;
		}
		
		public function getParent():IContext
		{
			return this._parent;
		}
		
		public function getNames():Object
		{
			var result:Object;
			if (this._parent)
				result = Context.merge(
					this._names, this._parent.getNames());
			else result = this._names;
			return result;
		}
		
		public function setParent(parent:IContext):IContext
		{
			if (this._parent) parent.setParent(this._parent);
			return this._parent = parent;
		}
		
		public function put(name:String, value:Object=null):void
		{
			throw "Cannot add properties to LogicContext";
		}
		
		public function resolve(name:String):Object
		{
			return this._names[name];
		}
	}
}